﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Kopytko.BoardGame
{
    /// <summary>
    /// Interaction logic for BoardBox.xaml
    /// </summary>
    public partial class BoardBox : UserControl
    {
        public event EventHandler Selected;

        public void InvokeSelected(EventArgs e)
        {
            EventHandler handler = Selected;
            if (handler != null) handler(this, e);
        }


        public Brush BoardBoxBg
        {
            get { return (Brush)GetValue(BoardBoxBgProperty); }
            set { SetValue(BoardBoxBgProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BoardBoxBg.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoardBoxBgProperty =
            DependencyProperty.Register("BoardBoxBg", typeof(Brush), typeof(BoardBox), new UIPropertyMetadata(Brushes.Transparent));


        public byte X { get; set; }
        public byte Y { get; set; }

        public BoardBox(byte x, byte y)
        {
            InitializeComponent();
            X = x;
            Y = y;
        }

        private bool isHighlighted;

        public bool IsHighlighted 
        { 
            get { return isHighlighted; } 
            set 
            { 
                isHighlighted = value;
                if (value)
                {
                    BoardBoxBg = FindResource("highlightedBrush") as Brush;
                }
                else
                {
                    BoardBoxBg = Brushes.Transparent;
                }
            }
        }

        private bool isSelected;

        public bool IsSelected
        {
            get { return isSelected; }
            set 
            { 
                isSelected = value;
                if (value)
                {
                    BoardBoxBg = FindResource("selectedBrush") as Brush;
                }
                else
                {
                    BoardBoxBg = Brushes.Transparent;
                }
            }
        }


        private void UserControl_MouseEnter(object sender, MouseEventArgs e)
        {
            if (isSelected || isHighlighted)
                return;
            BoardBoxBg = FindResource("mouseOverBrush") as Brush;
        }

        private void UserControl_MouseLeave(object sender, MouseEventArgs e)
        {
            if (isSelected || isHighlighted)
                return;
            BoardBoxBg = Brushes.Transparent;
        }

        private void self_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Selected(this, new EventArgs());
        }
    }
}
